package com.defysoft.waukidsbus.main;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukidsbus.BaseActivity;
import com.defysoft.waukidsbus.R;

public class Main_login extends BaseActivity {

	EditText name;
	EditText code;
	TextView btn;
	final int SUCCESS = 10;
	final int FAIR = 9;

	private SharedPreferences login_code;
	private SharedPreferences.Editor editor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_login);
		setStartData();
	}

	@Override
	public void setStartData() {
		// TODO Auto-generated method stub
		super.setStartData();
		login_code = getSharedPreferences("login_code", MODE_PRIVATE);
		editor = login_code.edit();
		name = (EditText) findViewById(R.id.MAIN_LOGIN_NAME);
	
		code = (EditText) findViewById(R.id.MAIN_LOGIN_CODE);
		name.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER) {
					InputMethodManager mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
					mInputMethodManager.hideSoftInputFromWindow(code.getWindowToken(), 0);
					code.requestFocus();
					return true;
				}
				return false;
			}
		});
		code.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER) {
					InputMethodManager mInputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
					mInputMethodManager.hideSoftInputFromWindow(code.getWindowToken(), 0);
					return true;
				}
				return false;
			}
		});
		btn = (TextView) findViewById(R.id.MAIN_LOGIN_BTN);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (name.getText().toString().length() == 0) {
					Toast.makeText(Main_login.this, "유치원 이름을 입력하세요",
							Toast.LENGTH_SHORT).show();
				} else if (code.getText().toString().length() == 0) {
					Toast.makeText(Main_login.this, "유치원 코드를 입력하세요",
							Toast.LENGTH_SHORT).show();
				} else {
					startNetwork(1);
				}
			}
		});
	}

	@Override
	public void startThread(int choice) {
		// TODO Auto-generated method stub
		super.startThread(choice);
		firstThread = new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				mainThread();
			}
		});
		firstThread.start();
		startDIal();
	}

	@Override
	protected void handlingMessage(Message _message) {
		// TODO Auto-generated method stub
		super.handlingMessage(_message);
		switch (_message.what) {
		case CATCH_EXCEPTION:
			endDIal();
			if (firstThread == null) {
				firstThread.interrupt();
			}
			showPopup("네트워크오류", "네트워크 실패", 0);
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_SHORT)
					.show();
			break;
		case FAIR:
			endDIal();
			if (firstThread == null) {
				firstThread.interrupt();
			}
			Toast.makeText(this, (String) _message.obj, Toast.LENGTH_SHORT)
					.show();
			break;
		case SUCCESS:
			endDIal();
			if (firstThread == null) {
				firstThread.interrupt();
			}
			editor.putString("sch_code", code.getText().toString());
			editor.putString("sch_name", name.getText().toString());
			editor.commit();

			startActivity(new Intent(this, Main_main.class));
			finish();
			break;
		}
	}

	protected void mainThread() {
		// TODO Auto-generated method stub
		String msg = "";
		try {
			JSONObject jsonObject = new JSONObject(publicHttpCallback(
					HTTP_LOGIN_CHECK, "name=" + name.getText().toString()
							+ "&code=" + code.getText().toString()));
			if (jsonObject.getBoolean("success")) {
				msg = jsonObject.getString("payload");
				onNetworkCallback(SUCCESS, msg);
			} else {
				onNetworkCallback(FAIR, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(CATCH_EXCEPTION, e.toString());
		}
		;
	}

}
